﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
namespace DAL
{
    public class GioHangDAL
    {
        public List<ChiTietGioHangDAL> DanhSachSanPham { get; set; }
        public static readonly GioHangDAL TheHien;
        static GioHangDAL() 
        {
            if (HttpContext.Current.Session["ASPNETGioHang"] == null)
            {
                TheHien = new GioHangDAL();
                TheHien.DanhSachSanPham = new List<ChiTietGioHangDAL>();
                HttpContext.Current.Session["ASPNETGioHang"] = TheHien;
            }
            else
            {
                TheHien = (GioHangDAL)HttpContext.Current.Session["ASPNETGioHang"];
            }
        }
        protected GioHangDAL() { }
        public void ThemSanPham(int _idSanPham)
        {
            ChiTietGioHangDAL _chiTiet = new ChiTietGioHangDAL(_idSanPham);
            if (DanhSachSanPham.Contains(_chiTiet))
            {
                foreach (ChiTietGioHangDAL item in DanhSachSanPham)
                {
                    if (item.SoSanh(_chiTiet))
                    {
                        item.SoLuong++;
                        return;
                    }
                }
            }
            else
            {
                _chiTiet.SoLuong = 1;
                DanhSachSanPham.Add(_chiTiet);
            }
        }
        public void DatSoLuongChiTietGioHang(int _idSanPham, int _soLuong)
        {
            if (_soLuong == 0)
            {
                XoaChiTietGioHang(_idSanPham);
                return;
            }
            ChiTietGioHangDAL _chiTietCapNhat = new ChiTietGioHangDAL(_idSanPham);
            foreach (ChiTietGioHangDAL item in DanhSachSanPham)
            {
                if (item.SoSanh(_chiTietCapNhat))
                {
                    item.SoLuong = _soLuong;
                    return;
                }
            }
        }
        public void XoaChiTietGioHang(int _intID)
        {
            ChiTietGioHangDAL _xoa = new ChiTietGioHangDAL(_intID);
            DanhSachSanPham.Remove(_xoa);
        }
        public decimal LayTongGiaTien()
        {
            decimal _tongTien = 0;
            foreach (ChiTietGioHangDAL item in DanhSachSanPham)
            {
                _tongTien += item.TongGiaTien;
            }
            return _tongTien;
        }
    }
}
